home *** CD-ROM | disk | FTP | other *** search
- unit HiddenWordThread;
-
- interface
-
- uses SysUtils,Classes,syncobjs,Windows,Messages,Dialogs,
- //My Units
- BasicThread;
-
- type
- THiddenWordThread=class(TBasicThread)
- private
- fHiddenWord:string;
- fSenderThreadID:THandle;
- procedure ExecuteLoop;
- procedure PeekMessage;
- procedure RequestWordLength;
- procedure LetterUsedAtPosition(const lp:LPARAM);
- protected
- procedure Execute;override;
- public
- constructor Create(CreateSuspended: Boolean; const HiddenWord: string);
- destructor Destroy;override;
- end;
-
- implementation
-
- constructor THiddenWordThread.Create
- (CreateSuspended: Boolean
- ;const HiddenWord:string);
- begin
- fHiddenWord:=HiddenWord;
- inherited Create(CreateSuspended);
- end;
-
- destructor THiddenWordThread.Destroy;
- begin
- inherited Destroy;
- end;
-
- procedure THiddenWordThread.Execute;
- var
- Msg:TMsg;
- begin
- try
- //Force a message queue for the thread to be created.
- Windows.PeekMessage(Msg,0,0,0,PM_NOREMOVE);
- ExecuteLoop;
- except
- fException:=ExceptObject As Exception;
- Synchronize(ShowException);
- end;
- end;
-
- procedure THiddenWordThread.ExecuteLoop;
- begin
- while not Terminated do
- begin
- try
- Windows.WaitMessage;
- //Note could use GetMessage but this wouldn't allow the WM_Quit to jump its
- //position in the queue.
- if PeekQuitMessage then
- exit;
- PeekMessage;
- except
- //Don't reraise the exception cos we require the loop to keep executing.
- on E:Exception do
- begin
- {Log Exception to Event Log}
- end;
- end;
- end;
- end;
-
- procedure THiddenWordThread.PeekMessage;
- var
- Msg:TMsg;
- begin
- while(Windows.PeekMessage(Msg,0,0,0,PM_Remove))do
- begin
- fSenderThreadID:=Msg.wParam;//Doesn't apply to WM_QUIT message
- case Msg.message of
- WM_QUIT:Terminate;
- WM_Signal_RequestWordLength:RequestWordLength;
- WM_Signal_RequestLetterUsedAtPosition:LetterUsedAtPosition(Msg.lParam);
- else
- begin
- exit;
- //Translates virtual-key messages into character messages
- //Not really required since the thread does not process any keyboard characters!!!
- TranslateMessage(Msg);
- //Dispatches message to a window procedure with the window handle specified in the MSG structure
- //Note again the window handle will be null and no action taken.
- DispatchMessage(Msg);
- end;
- end;
- end;
- end;
-
- procedure THiddenWordThread.RequestWordLength;
- begin
- PostThreadMessage(fSenderThreadID,WM_Signal_WordLengthIs,0,Length(fHiddenWord));
- end;
-
- procedure THiddenWordThread.LetterUsedAtPosition(const lp:LPARAM);
- var
- iLetter:integer;
- Position:integer;
- s:string;
- begin
- Position:=lp and 31;
- iLetter:=lp shr 5;
- s:=Char(iLetter);
- if CompareText(fHiddenWord[Position],s)=0then
- PostThreadMessage(fSenderThreadID,WM_Signal_Yes,0,0)
- else
- PostThreadMessage(fSenderThreadID,WM_Signal_No,0,0);
- end;
-
-
- end.
-